
package linuxbox.bbs.bean;

import java.util.*;

import linuxbox.exception.*;
import linuxbox.util.*;
import linuxbox.bean.*;
import linuxbox.bbs.exception.*;
import linuxbox.bbs.logic.*;

public class BbsCatBean extends TableBean {
  private	int m_iCatID = 0;
  private	String m_sCatName = "";

  public BbsCatBean(BbsCatKey pk, DbAdapter db) {
    this.init(pk, db);
  }

  public BbsCatBean(BbsCatKey pk) {
    this.init(pk, null);
  }

  private	void init(BbsCatKey pk, DbAdapter db) {
    super.init(db);
    if (pk != null)
      m_iCatID = pk.m_iCatID;
    super.setTableName("bbs_cat");
  }

  protected void load() throws BbsCatBeanException {
    if (!m_bLoaded) {
      try {
//== need to modify=========================================
        String sql;
        makeConnection();
        sql = "select cat_id, cat_name from bbs_cat where cat_id = " + m_iCatID;
        m_db.executeQuery(sql);

        if (m_db.next()) {
          m_sCatName = m_db.getString("cat_name");
        } else {
          close();
          throw new BbsCatBeanException("Load error:  m_iCatID not found!");
        }
        m_bLoaded = true;
        close();
//==end================================================
      } catch(DbBeanException e1) {
        throw new BbsCatBeanException("Can't load the record: " + e1.getMessage());
      } catch(TableBeanException e2) {
        throw new BbsCatBeanException("Can't load the record: " + e2.getMessage());
      }
    }
  }

  public void store() throws BbsCatBeanException {
    try {
//==need to modify=========================================
      String where = " cat_id = " + m_iCatID;
//==end================================================
      super.updateTable(where);
    } catch(TableBeanException e2) {
      throw new BbsCatBeanException("Can't update in store: " + e2.getMessage());
    }
  }

  public void refresh() throws BbsCatBeanException {
    m_bLoaded = false;
    load();
  }

  protected void create() throws BbsCatBeanException {
    try {
//==need to modify=========================================
      if (m_iCatID != 0) {
        String sql = "INSERT INTO bbs_cat(cat_id, cat_name) VALUES(" + m_iCatID + ", ' ')";
        makeConnection();
        m_db.executeUpdate(sql);
      } else {
        String sql = "select max(cat_id) from bbs_cat";
        makeConnection();
        m_db.executeQuery(sql);

        if (m_db.next()) {
          int max_id = m_db.getInt(1);
          max_id++;
          m_iCatID = max_id;
          sql = "insert into bbs_cat(cat_id, cat_name) values(" + max_id + ", ' ')";
          m_db.executeUpdate(sql);
        }
      }

      close();
//==end================================================
    } catch(DbBeanException e1) {
      throw new BbsCatBeanException("Can't create the Police record: " + e1.getMessage());
    } catch(TableBeanException e2) {
      throw new BbsCatBeanException("Can't create the Police record: " + e2.getMessage());
    }
  }

  protected void create(BbsCat cat) throws BbsCatBeanException {
    try {
      int id = cat.getID();
      String name = cat.getCatName();

      if (id != -1) {
        String sql = "INSERT INTO bbs_cat(cat_id, cat_name) VALUES("
          + id + ", ' ')";
        makeConnection();
        m_db.executeUpdate(sql);
      } else {
        String sql = "select max(cat_id) from bbs_cat";
        makeConnection();
        m_db.executeQuery(sql);

        if (m_db.next()) {
          int max_id = m_db.getInt(1);
          max_id++;
          m_iCatID = max_id;
          sql = "insert into bbs_cat(cat_id, cat_name) values("
            + max_id + ", '" + name + " ')";
          m_db.executeUpdate(sql);
        }
      }

      close();
//==end================================================
    } catch(DbBeanException e1) {
      throw new BbsCatBeanException("Can't create the Police record: " + e1.getMessage());
    } catch(TableBeanException e2) {
      throw new BbsCatBeanException("Can't create the Police record: " + e2.getMessage());
    }
  }

  protected void remove() throws BbsCatBeanException {
    try {
//==need to modify=========================================
      String sql = "DELETE from bbs_cat WHERE cat_id = " + m_iCatID;
      makeConnection();
      m_db.executeUpdate(sql);
      close();
//==end================================================
    } catch(DbBeanException e1) {
      throw new BbsCatBeanException("Can't delete the Police record: " + e1.getMessage());
    } catch(TableBeanException e2) {
      throw new BbsCatBeanException("Can't delete the Police record: " + e2.getMessage());
    }
  }

  protected BbsCatKey findByKey() throws BbsCatBeanException {
    BbsCatKey pk = null;
    try {
//==modify=========================================
      String sql = "select cat_id from bbs_cat where cat_id = " + m_iCatID;
      makeConnection();
      m_db.executeQuery(sql);

      if (m_db.next()) {
        pk = new BbsCatKey();
        pk.setCatID(m_db.getInt(1));
      }
      close();
//==end================================================
    } catch(DbBeanException e1) {
      throw new BbsCatBeanException("Can't find the Police record: " + e1.getMessage());
    } catch(TableBeanException e2) {
      throw new BbsCatBeanException("Can't find the Police record: " + e2.getMessage());
    }
    return pk;
  }

  protected Vector findBySql(String sqlWhere) throws BbsCatBeanException {
    Vector v = null;
    try {
//==need to modify=========================================
      String sql = "select cat_id from bbs_cat " + sqlWhere;
      makeConnection();
      m_db.executeQuery(sql);
      BbsCatKey pk = null;
      v = new Vector();
      while (m_db.next()) {
        pk = new BbsCatKey();
        pk.setCatID(m_db.getInt(1));
        v.addElement(pk);
      }
      close();
//==end================================================
    } catch(DbBeanException e1) {
      throw new BbsCatBeanException("Can't find record: " + e1.getMessage());
    } catch(TableBeanException e2) {
      throw new BbsCatBeanException("Can't find record: " + e2.getMessage());
    }
    return v;
  }

  public int getCatID() {
    return m_iCatID;
  }

  public String getCatName() throws BbsCatBeanException {
    load();
    return m_sCatName;
  }

  public void setCatName(String str) throws BbsCatBeanException {
    m_sCatName = str;
    setString("cat_name", str);
  }
}
